<?php
/**
 * 周舟 hzboye010@163.com
 * addby sublime snippets
 */
namespace Admin\Controller;

use Common\Api\GameApi;
// use Admin\Model\GameModel;
use Admin\Controller\AdminController;

class GameController extends AdminController{
    // protected  $model;
    // protected  $url;

    protected function _initialize() {
        parent::_initialize();
        // $this->model = D('Game');
        // $this->url = U('Admin/Game/index', array());
    }

    /**
     * show game list
     * @return [tpl]
     */
    public function index($p=1){
        $this->assignTitle('抽奖管理');

        $r = apiCall(GameApi::PAGE,array(array(),array('curpage'=>$p,'size'=>10)));
        if($r['status'])
        {
            $list = $r['info']['list'];
            foreach ($list as $k => $v) {
                $list[$k]['desc'] = mb_substr(htmlspecialchars_decode($v['description']),0,20);
            }
            $this->assign('list',$list);
            $this->assign('show',$r['info']['show']);
        }else{
            $this->error('GameAPI:Page:ERROR');
        }
        $this->display(); // 输出模板
    }

    /**
     * delete game by id
     * @param  [int]    $gid
     * @return [type]
     */
    public function delete($gid){
        if(intval($gid) && $gid)
        {
           $r = apiCall(GameApi::DEL,array(array('id'=>$gid)));
           if($r['status'])
           {
                $this->success(L('RESULT_SUCCESS'),U('Game/index'));
           }else{
                $this->error('删除失败');
           }
        }else{
            $this->error('非法操作');
        }
    }

    /**
     * update game by id
     * @param  [int]    gid
     * @return [type]
     */
    public function edit($gid){
        $game = $this->getGameById($gid);
        $this ->assignTitle($game['name'].' - 抽奖编辑');
        $this ->assign('game',$game);
        if(IS_POST)
        {
            $map = I('post.');
            if(strtotime($map['start_time']) > strtotime($map['end_time']))
                $this->error('错误抽奖区间');
            $r = apiCall(GameApi::SAVEBYID,array($gid,$map));
            if($r['status']){
                $this->success(L('RESULT_SUCCESS'),U('Game/index', array('gid'=>$gid)));
            }else{
                // dump($r['info']);exit;
                // $this->error($r['info']);
                $this->error('保存失败');
            }
        }
        $this->display();
    }

    /**
     * post: add the post game
     * get : show the add page
     */
    public function add(){
        if(IS_POST){
            $r = apiCall(GameApi::ADD,array(I('post.')));
            if($r['status'])
            {
                $this->success(L('RESULT_SUCCESS'),U('Game/index'));
            }else{
                $this->error('添加失败');
            }
        }
        $this->assignTitle('添加抽奖');
        $this->display();
    }

    /**
     * 检测抽奖名是否已存在
     * @param  [string]  the posted game name
     * @return [string]  [0 fail /1 success]
     */
    public function check_name($name){
        // if(IS_POST){
        //     $where['name'] = I('post.name','','trim');
        //     $game = D('game')->where($where)->find();
        //     if($game)  return '0'; //失败
        // }
        return '1'; //OK
    }


    public function search($kword='',$p=1){
        $this->assignTitle($kword.' - 搜索结果');
        $where = array();
        if($kword){
            $where['id'] = $kword;
            $where['name']   = array('like','%'.$kword.'%');
            $where['_logic'] = 'OR';
        }
        $r = apiCall(GameApi::PAGE,array($where,array('curpage'=>$p,'size'=>10)));
        if($r['status'])
        {
            $list = $r['info']['list'];
            foreach ($list as $k => $v) {
                $list[$k]['desc'] = mb_substr(htmlspecialchars_decode($v['description']),0,20);
            }
            $this->assign('list',$list);
            $this->assign('show',$r['info']['show']);
        }else{
            $this->error('GameAPI:Page:ERROR');
        }
        $this->display('index');
    }

    protected function getGameById($gid){
        $r = apiCall(GameApi::FIND,array(array('id'=>$gid)));
        if(!$r || !$r['status']) $this->error('GameApi:find:error');
        // if(!$r['info']) $this->error('错误抽奖');
        return $r['info'];
    }
}

